using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pong
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Ball : Actor
    {
        private Vector2 velocity;
        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        private Vector2 startingVelocity;
        public Vector2 StartingVelocity
        {
            get { return startingVelocity; }
            set { startingVelocity = value; }
        }

        Game1 mainReference;

        public Ball(Game1 game)
            : base(game)
        {
            mainReference = game;
            mainReference.Components.Add(this);
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            Random rand = new Random();
            int modX = rand.Next(2);

            if (modX == 0)
            {
                modX = -1;
            }

            int modY = rand.Next(2);

            if (modY == 0)
            {
                modY = -1;
            }

            this.velocity = new Vector2(modX * 2, modY * 3);
            this.startingVelocity = velocity;
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        //The ball is controlled by the computer
        public void Move()
        {
            //make the ball move
            position.X += velocity.X;
            position.Y += velocity.Y;

            //When it touches the FLOOR, CEILING or a PADDLE 
                //it bounces by making the velocity equal to neg velocity
            if (position.Y >= mainReference.screenHeight - source.Height || position.Y <= 0)
            {
                position -= velocity;
                velocity.Y = -velocity.Y;
                IncreaseVelocity();
            }
            if (Colliding(mainReference.paddle1) || Colliding(mainReference.paddle2))
            {
                position -= velocity;
                velocity.X = -velocity.X;
                IncreaseVelocity();
            }

            EndGame();

            //if (true)
            //{
                //When the ball exits on the sides of the screen 
                //then one of the players loses according to which side it left
                //EndGame();
            //} 

            //When it touches a paddle
            //IncreaseVelocity();
        }
        private void EndGame()
        {
            if (position.X >= mainReference.screenWidth - source.Width || position.X <= 0)
            {
                mainReference.gameStarted = false;
            }
        }

        //makes the ball go faster that it did previously
        private void IncreaseVelocity()
        {
            velocity.X += 0.05f;
            velocity.Y += 0.05f;
        }

        public void Respawn()
        {
            mainReference.UnloadContentWrapper();
            mainReference.InitializeWrapper();
        }
    }
}
